package cn.yzgy.yzgyoa.ucenter.dto;

import cn.yzgy.yzgyoa.ucenter.entity.SysMenu;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单项传输对象
 *
 * @author Yqsen
 * @since 2019-12-06 16:20:05
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class MenuItemDto {

    @NotNull
    @Range(min = 0, max = 9999)
    private long id;

    @NotNull
    private String title;
    private String url;

    @NotNull
    private String icon;
    private boolean spread;

    @NotNull
    @Range(min = 0, max = 99)
    private int sort;

    @NotNull
    @Range(min = 0, max = 9999)
    private long parentMid;

    @NotNull
    @Range(min = 0, max = 9999)
    private long permId;
    private String permVal;
    private List<MenuItemDto> children;

    /**
     * 将SysMenu对象转换为便于传输的DTO格式
     *
     * @param menus SysMenu对象
     * @return DTO
     */
    public static List<MenuItemDto> trans(List<SysMenu> menus) {
        return menus.stream()
                .map(x -> new MenuItemDto()
                        .setId(x.getMid())
                        .setTitle(x.getTitle())
                        .setSpread(true)
                        .setUrl(x.getUrl())
                        .setIcon(x.getIcon())
                        .setSort(x.getSort())
                        .setParentMid(x.getParentMid())
                        .setPermId(x.getPermId())
                        .setPermVal(x.getPermVal())
                        .setChildren(trans(x.getChilds())))
                .collect(Collectors.toList());
    }

    /**
     * 将DTO格式的菜单对象转换为SysMenu对象
     *
     * @param menuItemDto DTO
     * @return SysMenu对象
     */
    public static SysMenu getSysMenu(MenuItemDto menuItemDto) {
        return new SysMenu()
                .setMid(menuItemDto.getId())
                .setTitle(menuItemDto.getTitle())
                .setUrl(menuItemDto.getUrl())
                .setIcon(menuItemDto.getIcon())
                .setParentMid(menuItemDto.getParentMid())
                .setPermId(menuItemDto.getPermId())
                .setSort(menuItemDto.getSort());
    }

}
